﻿using System;
using System.Collections.Generic;
using System.Text;
using YXH_Tools_Files.Tools_Error;

namespace RainFlowandDamageTool.Test
{
    public static class ReadCSV
    {
        public static async Task<List<List<double>>> YXHReadColumnsfromCSV2List(string filepath, List<int> selectcollumnnumber)
        {

            List<List<double>> selectlists = new List<List<double>>();
            for (int i = 0; i < selectcollumnnumber.Count; i++)
            {
                var list = new List<double>();
                selectlists.Add(list);
            }
            await Task.Run(() => {
                if (File.Exists(filepath))
                {
                    Encoding encoding = Encoding.Default;

                    //string starttime = name.Split('-')[1];
                    using FileStream fs = new FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite);
                    StreamReader sr = new StreamReader(fs, encoding);
                    string? strLine = "";
                    //记录每行记录中的各字段内容
                    string[]? aryLine = null;
                    string[]? tableHead = null;
                    int columnCount = 0;
                    ////标示列数
                    //int columnCount = 0;

                    //标示是否是读取的第一行
                    bool IsFirst = true;

                    while ((strLine = sr.ReadLine()) != null)
                    {
                        if (IsFirst == true)
                        {
                            //改为小写，去掉下划线，空格和N
                            tableHead = strLine.Replace("_", "").Replace("N", "").Replace(" ", "").ToLower().Split(',');
                            IsFirst = false;
                            columnCount = tableHead.Length;


                        }
                        else
                        {
                            //说明选取的collumn超过了原有的数量
                            for (int i = 0; i < selectcollumnnumber.Count; i++)
                            {
                                if (selectcollumnnumber[i] >= 0 || selectcollumnnumber[i] < columnCount)
                                {
                                    aryLine = strLine.Split(',');
                                    var success = double.TryParse(aryLine[selectcollumnnumber[i]], out double number);
                                    if ((!double.IsNaN(number)))
                                    {
                                        selectlists[i].Add(number);
                                    }
                                    else
                                    {
                                        LogErrorMessage.getErrormessage(filepath, $"列号为: {selectcollumnnumber[i]}有nan或空白");
                                        //Console.WriteLine($"列号为: {selectcollumnnumber[i]}有nan或空白");
                                        //selectlists[i].Add(0);
                                    }
                                }
                                else
                                {
                                    LogErrorMessage.getErrormessage(filepath, $"列号超出界限: {selectcollumnnumber[i]}");
                                    // Console.WriteLine($"列号超出界限: {selectcollumnnumber[i]}");
                                    //throw new Exception($"列号超出界限: {selectcollumnnumber[i]}");
                                }
                            }

                        }
                    }
                    sr.Close();
                    sr.Dispose();
                    fs.Close();
                    fs.Dispose();

                    //存在 
                }
                else
                {
                    LogErrorMessage.getErrormessage(filepath, $"文件不存在");
                }

            });
            return selectlists;
        }
        public static void YXHAppendData2CSV(string filepath, string data)
        {
            if (!File.Exists(filepath))
            {
                // 创建新的CSV文件并写入表头
                using (StreamWriter sw = File.CreateText(filepath))
                {
                    sw.WriteLine("name,maxvariance,meanvariance,ratio");
                }
            }

            // 追加数据到CSV文件
            using (StreamWriter sw = File.AppendText(filepath))
            {
                sw.WriteLine(data);
            }

        }
    }
}
